package la.iok.hzsvn.lewin.movie.shopmovie.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import la.iok.hzsvn.lewin.movie.core.RoleEnum;
import la.iok.hzsvn.lewin.movie.core.Token;
import la.iok.hzsvn.lewin.movie.core.exception.ErrorCode;
import la.iok.hzsvn.lewin.movie.core.model.SimpleOrgUnitVo;
import la.iok.hzsvn.lewin.movie.core.model.SimpleUser;
import la.iok.hzsvn.lewin.movie.core.model.UserVo;
import la.iok.hzsvn.lewin.movie.core.shopmovie.param.CouponUseRecordListParam;
import la.iok.hzsvn.lewin.movie.core.shopmovie.vo.CouponUseRecordVo;
import la.iok.hzsvn.lewin.movie.core.shopmovie.vo.PlayerFeeCoupon;
import la.iok.hzsvn.lewin.movie.core.shopmovie.vo.PlayerFeeVo;
import la.iok.hzsvn.lewin.movie.services.MovieServiceInvoker;
import la.iok.hzsvn.lewin.movie.services.OrgUnitIntegration;
import la.iok.hzsvn.lewin.movie.shopmovie.entity.*;
import la.iok.hzsvn.lewin.movie.shopmovie.mapper.CouponUseRecordMapper;
import la.iok.hzsvn.lewin.mybatis.model.PageParam;
import la.iok.hzsvn.lewin.mybatis.model.PageVo;
import la.iok.hzsvn.lewin.mybatis.model.SortParam;
import la.iok.hzsvn.lewin.mybatis.service.BaseServiceImpl;
import la.iok.hzsvn.share.annotations.NotNull;
import la.iok.hzsvn.share.annotations.NotNullElement;
import la.iok.hzsvn.share.annotations.Nullable;
import la.iok.hzsvn.share.collection.IdOperators;
import la.iok.hzsvn.share.phone.PhoneNumberUtils;
import la.iok.hzsvn.share.protocol.exception.ErrorCodeException;
import org.apache.catalina.User;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class CouponUseRecordServiceImpl extends BaseServiceImpl<CouponUseRecord, CouponUseRecordMapper, Long> implements CouponUseRecordService {
    private final CarpoolService carpoolService;
    private final CouponService couponService;
    private final ShopMovieService shopMovieService;
    private final PlayerService playerService;
    private final PlayerFeeService playerFeeService;
    private final OrgUnitIntegration orgUnitIntegration;
    private final MovieServiceInvoker movieServiceInvoker;


    public CouponUseRecordServiceImpl(CouponUseRecordMapper mapper,
                                      CarpoolService carpoolService,
                                      CouponService couponService,
                                      ShopMovieService shopMovieService,
                                      PlayerService playerService,
                                      PlayerFeeService playerFeeService,
                                      OrgUnitIntegration orgUnitIntegration,
                                      MovieServiceInvoker movieServiceInvoker
                                      ) {
        super(mapper);
        this.carpoolService = carpoolService;
        this.couponService = couponService;
        this.shopMovieService = shopMovieService;
        this.playerService = playerService;
        this.playerFeeService = playerFeeService;
        this.orgUnitIntegration = orgUnitIntegration;
        this.movieServiceInvoker = movieServiceInvoker;
    }

    @Transactional
    @Override
    public Long save(Token token, Long carpoolId, Long couponId) {
        Carpool carpool = carpoolService.select(carpoolId);
        ErrorCode.PROPERTY_CHECK_ERROR.assertNotNull(carpool, ()->new Object[]{"拼车","拼车["+carpoolId+"]不存在"});
        Coupon coupon = couponService.select(couponId);
        ErrorCode.PROPERTY_CHECK_ERROR.assertNotNull(coupon, ()->new Object[]{"优惠券","优惠券[" + couponId + "]不存在"});
        //ErrorCode.PROPERTY_CHECK_ERROR.assertEqual(carpool.getOrgUnitId(), coupon.getOrgUnitId(),"优惠券","非本店铺发放优惠券不能使用");
        ErrorCode.PROPERTY_CHECK_ERROR.assertEqual(token.getUserId(), coupon.getUserId(),"用户","优惠券不属于当前用户");
        ErrorCode.PROPERTY_CHECK_ERROR.assertFalse(coupon.statusUsed(),"状态","优惠券已经使用");
        if(!coupon.applyAllLevelMovie()){
            ShopMovie shopMovie = shopMovieService.select(carpool.getShopMovieId());
            ErrorCode.PROPERTY_CHECK_ERROR.assertNotNull(shopMovie, ()->new Object[]{"剧本","店铺剧本[" + carpool.getShopMovieId()+"]不存在"});
            ErrorCode.PROPERTY_CHECK_ERROR.assertEqual(coupon.getMovieLevelId(), shopMovie.getMovieLevelId(),"适用范围","优惠券不适用当前剧本");
        }
        Player player = playerService.select(carpoolId, token.getUserId());
        ErrorCode.PROPERTY_CHECK_ERROR.assertNotNull(player,"玩家","玩家未加入拼车");
        ErrorCode.PROPERTY_CHECK_ERROR.assertFalse(player.virtualType(),"玩家","虚拟玩家不能使用优惠券");
        PlayerFee playerFee = playerFeeService.movieFee(player.getId());
        ErrorCode.PROPERTY_CHECK_ERROR.assertNotNull(playerFee,"剧本费","未查询到玩家的剧本费");
        ErrorCode.PROPERTY_CHECK_ERROR.assertFalse(playerFee.paid(),"剧本费","玩家剧本费已经支付");
        ErrorCode.PROPERTY_CHECK_ERROR.assertEqual(0,playerFee.getCouponDeductionAmount(),"优惠券",
                "当前剧本已经使用过优惠券,不能多次使用");
        int amount = coupon.getAmount();
        if(coupon.typePercent()){
            amount = (playerFee.getFeeAmount() * coupon.getAmount()) / 100;
            if(!coupon.unlimitedAmount()){
                amount = Math.min(amount, coupon.getMaxAmount());
            }
        }else{
            amount = Math.min(amount, playerFee.getFeeAmount());
        }
        ErrorCode.RECORD_MODIFIED_ERROR.assertEqual(1,couponService.use(coupon),couponId);
        ErrorCode.RECORD_MODIFIED_ERROR.assertEqual(1,playerFeeService.addCouponDeductionAmount(playerFee.getId(),
                playerFee.getVersion(), amount), playerFee.getId());
        return doSave(carpool.getOrgUnitId(), token.getUserId(), couponId, carpoolId, playerFee.getId(), amount);
    }


    @Override
    public Long choice(Token token, Long carpoolId, Long couponId,Long playerFeeID) {

        //确定折扣车次对象
        Carpool carpool = carpoolService.select(carpoolId);
        ErrorCode.PROPERTY_CHECK_ERROR.assertNotNull(carpool, ()->new Object[]{"拼车","拼车["+carpoolId+"]不存在"});
        //确定券存在性以及状态
        Coupon coupon = couponService.select(couponId);
        ErrorCode.PROPERTY_CHECK_ERROR.assertNotNull(coupon, ()->new Object[]{"优惠券","优惠券[" + couponId + "]不存在"});

        //确定是自己用券or客服协助使用优惠券
        if(token.getUserId()!=coupon.getUserId())
        {
            if(!token.hasAnyAuthority(RoleEnum.Admin,RoleEnum.Manager,RoleEnum.ShopManager))
            {
                ErrorCode.PROPERTY_CHECK_ERROR.assertEqual(token.getUserId(), coupon.getUserId(),"权限","优惠券不属于当前操作用户");
            }
        }

        ErrorCode.PROPERTY_CHECK_ERROR.assertFalse(coupon.statusWaitLock(),"状态","优惠券已待锁定");
        ErrorCode.PROPERTY_CHECK_ERROR.assertFalse(coupon.statusUsed(),"状态","优惠券已经使用");
        ErrorCode.PROPERTY_CHECK_ERROR.assertFalse(coupon.statusLock(),"状态","优惠券已经锁定");

        //确定剧本存在以及适用范围
        if(!coupon.applyAllLevelMovie()){
            ShopMovie shopMovie = shopMovieService.select(carpool.getShopMovieId());
            ErrorCode.PROPERTY_CHECK_ERROR.assertNotNull(shopMovie, ()->new Object[]{"剧本","店铺剧本[" + carpool.getShopMovieId()+"]不存在"});
            ErrorCode.PROPERTY_CHECK_ERROR.assertEqual(coupon.getMovieLevelId(), shopMovie.getMovieLevelId(),"适用范围","优惠券不适用当前剧本");
        }

        PlayerFee playerFee =playerFeeService.select(playerFeeID);
        ErrorCode.PROPERTY_CHECK_ERROR.assertNotNull(playerFee,"剧本费","未查询到玩家的剧本费");
        Player player =playerService.select(playerFee.getPlayerId());
        Player tokenplayer=playerService.select(carpoolId, token.getUserId());
        /*
        if(token.getUserId()==coupon.getUserId()) {
            ErrorCode.PROPERTY_CHECK_ERROR.assertEqual(player.getCarpoolId(), tokenplayer.getCarpoolId(), "优惠券", "优惠券仅能共享给同车次玩家");
        }

         */


        //判断是否已提交过使用优惠劵
       CouponUseRecord SelectCouponUseRecord=selectCouponUseRecord(carpoolId,playerFee.getId());
      //  CouponUseRecord SelectCouponUseRecord=executeSelect("carpoolId=" + carpoolId + " and playerFeeId=" + playerFee.getId());
        if(SelectCouponUseRecord!=null) {
            //判断一下优惠劵是否为同一张
            if (!SelectCouponUseRecord.getCouponId().equals(couponId)){
                //获取原使用的优惠劵ID
                Coupon oldCoupon = couponService.select(SelectCouponUseRecord.getCouponId());
                ErrorCode.PROPERTY_CHECK_ERROR.assertNotNull(oldCoupon,"优惠券","未查询到该优惠券");
                ErrorCode.PROPERTY_CHECK_ERROR.assertEqual(4, oldCoupon.getStatus(),"优惠劵","已选择优惠券不为待锁定状态");
                //如果不是原优惠券使用者修改则判定是否为本人修改
                if(!(Objects.equals(token.getUserId(), oldCoupon.getUserId())))
                {
                    ErrorCode.PROPERTY_CHECK_ERROR.assertEqual(player.getUserId(), token.getUserId(),"优惠劵","无法修改优惠券");
                }
                SelectCouponUseRecord.setCouponId(couponId);

                ErrorCode.PROPERTY_CHECK_ERROR.assertNotNull(modifyCouponId(SelectCouponUseRecord),"修改优惠券","修改优惠券失败");
                ErrorCode.PROPERTY_CHECK_ERROR.assertNotNull(couponService.unWaitLock(oldCoupon),"优惠券状态","修改优惠券状态-解除带锁定-失败");
            }

        }
        int amount = coupon.getAmount();
        if(coupon.typePercent()){
            amount = (playerFee.getFeeAmount() * coupon.getAmount()) / 100;
            if(!coupon.unlimitedAmount()){
                amount = Math.min(amount, coupon.getMaxAmount());
            }
        }else{
            amount = Math.min(amount, playerFee.getFeeAmount());
        }
        //ErrorCode.RECORD_MODIFIED_ERROR.assertEqual(1,couponService.choice(coupon),couponId);

        ErrorCode.RECORD_MODIFIED_ERROR.assertEqual(1,playerFeeService.setCouponDeductionAmount(playerFee.getId(),
                playerFee.getVersion(), amount), playerFee.getId());
        ErrorCode.PROPERTY_CHECK_ERROR.assertNotNull(couponService.waitLock(coupon),"优惠券状态","修改优惠券状态-待锁定-失败");
        return choiceSave(carpool.getOrgUnitId(), token.getUserId(), couponId, carpoolId, playerFee.getId(), amount);
    }



    private Long choiceSave(Long orgUnitId, Long userId, Long couponId, Long carpoolId, Long playerFeeId, int amount)
    {
        CouponUseRecord couponUseRecord=selectCouponUseRecord(carpoolId,playerFeeId);
        if(couponUseRecord==null) {
            CouponUseRecord entity = new CouponUseRecord();
            entity.setOrgUnitId(orgUnitId);
            entity.setUserId(userId);
            entity.setAmount(amount);
            entity.setCouponId(couponId);
            entity.setCarpoolId(carpoolId);
            entity.setTime(new Date());
            entity.setPlayerFeeId(playerFeeId);
            insertSelective(entity);
            return entity.getId();
        }else {
            couponUseRecord.setOrgUnitId(orgUnitId);
            couponUseRecord.setUserId(userId);
            couponUseRecord.setAmount(amount);
            couponUseRecord.setCouponId(couponId);
            couponUseRecord.setCarpoolId(carpoolId);
            couponUseRecord.setTime(new Date());
            couponUseRecord.setPlayerFeeId(playerFeeId);
            updateSelective(couponUseRecord);
            return couponUseRecord.getId();
        }
    }

    private Object modifyCouponId(CouponUseRecord couponUseRecord)
    {
        return mapper.modifyCouponId(couponUseRecord.getId(), couponUseRecord.getCouponId(), couponUseRecord.getUserId());
    }

   public CouponUseRecord selectCouponUseRecord(Long carpoolId, Long playerFeeId)
    {
        CouponUseRecord selectCoupoUseRecord;
       if(mapper.selectCouponUseRecord(carpoolId, playerFeeId)!=null) {
           {
               selectCoupoUseRecord=mapper.selectCouponUseRecord(carpoolId, playerFeeId);
               return selectCoupoUseRecord;
           }
       }
        return null;
    }

    @Override
    public Long deleteCouponUseRecord(Token token, Long playerFeeId) {
        PlayerFee playFee=playerFeeService.select(playerFeeId);
        ErrorCode.PROPERTY_CHECK_ERROR.assertNotNull(playFee,"收费项","未查找到该收费项");

        Player player=playerService.select(playFee.getPlayerId());
        ErrorCode.PROPERTY_CHECK_ERROR.assertNotNull(player,"玩家","未查找到该玩家");

        CouponUseRecord couponUseRecord=selectCouponUseRecord(player.getCarpoolId(),playerFeeId);
        ErrorCode.PROPERTY_CHECK_ERROR.assertNotNull(couponUseRecord,"优惠券","未查找到该优惠券使用记录");

        Coupon coupon=couponService.select(couponUseRecord.getCouponId());
        ErrorCode.PROPERTY_CHECK_ERROR.assertFalse(coupon.statusUsed(),"状态","优惠券已经使用");
        ErrorCode.PROPERTY_CHECK_ERROR.assertFalse(coupon.statusLock(),"状态","优惠券已经锁定");
        ErrorCode.PROPERTY_CHECK_ERROR.assertEqual(4, coupon.getStatus(),"优惠劵","已选择优惠券不为待锁定状态");

        if(!Objects.equals(token.getUserId(), player.getUserId())) {
            if(!Objects.equals(token.getUserId(), coupon.getUserId())) {
                token.validAnyAuthority(RoleEnum.Admin, RoleEnum.Manager, RoleEnum.ShopManager);
            }
        }

        if(playFee.getCouponDeductionAmount()!=0)
        {
            playerFeeService.setCouponDeductionAmount(playFee.getId(),playFee.getVersion(),0);
        }
        couponService.unWaitLock(coupon);
        return mapper.deleteCouponUseRecord(couponUseRecord.getId());
    }


    private Long doSave(Long orgUnitId, Long userId, Long couponId, Long carpoolId, Long playerFeeId, Integer amount){
        CouponUseRecord entity = new CouponUseRecord();
        entity.setOrgUnitId(orgUnitId);
        entity.setUserId(userId);
        entity.setAmount(amount);
        entity.setCouponId(couponId);
        entity.setCarpoolId(carpoolId);
        entity.setTime(new Date());
        entity.setPlayerFeeId(playerFeeId);
        insertSelective(entity);
        return entity.getId();
    }


    @NotNull
    @Override
    public PageVo<CouponUseRecordVo> page(@NotNull Token token, @NotNull PageParam pageParam, @NotNull SortParam sortParam,
                                          @NotNull CouponUseRecordListParam listParam) {
        ErrorCode.PROPERTY_CHECK_ERROR.assertNotNull(token.getUserId(), "用户","只能登录用户调用");
        Set<Long> orgIds = new HashSet<>();
        Long userId = null;
        if(token.hasAuthority(RoleEnum.Admin)){
            //nothing
        }else if(token.hasAnyAuthority(RoleEnum.Manager, RoleEnum.ShopManager, RoleEnum.CarpoolMaster, RoleEnum.CustomerService, RoleEnum.DM)){
            orgUnitIntegration.userManageOrgUnits(token, orgIds);
            userId = token.getUserId();
        }else{
            userId = token.getUserId();
        }
        String orderColumn = sortParam.orderColumn("status");
        String order = sortParam.order("ASC");
        Page<CouponUseRecord> list = PageHelper.startPage(pageParam.getPage(),pageParam.getLimit());
        mapper.listByParam(order, orderColumn, listParam, orgIds, userId);
        Set<Long> orgUnitIds = list.stream().map(CouponUseRecord::getOrgUnitId).filter(Objects::nonNull).collect(Collectors.toSet());
        List<SimpleOrgUnitVo> orgUnits = movieServiceInvoker.orgUnits(orgUnitIds);

        Set<Long> userIds = list.stream().flatMap(item-> Stream.of(item.getUserId(), item.getConfirmUserId())).collect(Collectors.toSet());
        List<SimpleUser> users = movieServiceInvoker.userInfo(userIds);
        List<Carpool> carpools = carpoolService.listInProperty("id", list.stream().map(CouponUseRecord::getCarpoolId).filter(Objects::nonNull).collect(Collectors.toSet()));
        List<PlayerFee> fees = playerFeeService.listInProperty("id", list.stream().map(CouponUseRecord::getPlayerFeeId).filter(Objects::nonNull).collect(Collectors.toSet()));
        return page(list, item-> po2vo(item, orgUnits, carpools, fees,users));
    }

    @Override
    public PageVo<CouponUseRecordVo> getpage(Token token, PageParam pageParam, SortParam sortParam, CouponUseRecordListParam listParam) {
        ErrorCode.PROPERTY_CHECK_ERROR.assertNotNull(token.getUserId(), "用户","只能登录用户调用");
        Set<Long> orgIds = new HashSet<>();
        Long userId = null;
        if(token.hasAuthority(RoleEnum.Admin)){
            //nothing
        }else if(token.hasAnyAuthority(RoleEnum.Manager, RoleEnum.ShopManager, RoleEnum.CarpoolMaster, RoleEnum.CustomerService, RoleEnum.DM)){
            orgUnitIntegration.userManageOrgUnits(token, orgIds);
            userId = token.getUserId();
        }else{
            userId = token.getUserId();
        }
        String orderColumn = sortParam.orderColumn("status");
        String order = sortParam.order("ASC");
        Page<CouponUseRecord> list = PageHelper.startPage(pageParam.getPage(),pageParam.getLimit());
        mapper.SelectRecord(order, orderColumn, listParam, orgIds, userId);
        Set<Long> orgUnitIds = list.stream().map(CouponUseRecord::getOrgUnitId).filter(Objects::nonNull).collect(Collectors.toSet());
        List<SimpleOrgUnitVo> orgUnits = movieServiceInvoker.orgUnits(orgUnitIds);

        Set<Long> userIds = list.stream().flatMap(item-> Stream.of(item.getUserId(), item.getConfirmUserId())).collect(Collectors.toSet());
        List<SimpleUser> users = movieServiceInvoker.userInfo(userIds);
        List<Carpool> carpools = carpoolService.listInProperty("id", list.stream().map(CouponUseRecord::getCarpoolId).filter(Objects::nonNull).collect(Collectors.toSet()));
        List<PlayerFee> fees = playerFeeService.listInProperty("id", list.stream().map(CouponUseRecord::getPlayerFeeId).filter(Objects::nonNull).collect(Collectors.toSet()));
        return page(list, item-> po2vo(item, orgUnits, carpools, fees,users));
    }

    @Override
    public void confirm(@NotNull Token token, @Nullable Long id) {
        token.validAnyAuthority(RoleEnum.Manager, RoleEnum.ShopManager, RoleEnum.CarpoolMaster, RoleEnum.CustomerService, RoleEnum.DM);
        CouponUseRecord record = select(id);
        ErrorCode.RECORD_NOT_EXIST.assertNotNull(record, id);
        ErrorCode.ORG_UNIT_ACCESS_DENIED.assertEqual(record.getOrgUnitId(), token.getOrgUnitId(), record.getOrgUnitId());
        ErrorCode.PROPERTY_CHECK_ERROR.assertNotEquals(10, record.getStatus(),"状态","优惠券使用记录已经确认");
        record.setConfirmUserId(token.getUserId());
        record.setConfirmTime(new Date());
        ErrorCode.RECORD_MODIFIED_ERROR.assertEqual(1, mapper.confirm(record), id);
    }

    @Override
    @NotNull
    @NotNullElement
    public List<PlayerFeeVo> fees(Token token, Long carpoolId) {
        List<PlayerFee> fees = carpoolService.fees(token, carpoolId);
        if(fees.isEmpty()){
            return Collections.emptyList();
        }
        List<CouponUseRecord> list = listInProperty("playerFeeId", fees.stream().map(PlayerFee::getId).collect(Collectors.toSet()));
        List<Coupon> coupons = couponService.listInProperty("id", list.stream().map(CouponUseRecord::getCouponId).filter(Objects::nonNull).collect(Collectors.toSet()));
        return fees.stream().map(fee->{
            PlayerFeeVo vo = new PlayerFeeVo();
            BeanUtils.copyProperties(fee, vo);
            List<PlayerFeeCoupon> playerFeeCoupons = list.stream().filter(item->Objects.equals(item.getPlayerFeeId(), fee.getId())).map(ur->{
                PlayerFeeCoupon coupon = new PlayerFeeCoupon();
                coupon.setId(ur.getId());
                coupon.setCouponId(ur.getCouponId());
                coupon.setUserId(selectCouponUseRecord(carpoolId,ur.getPlayerFeeId()).getUserId());
                coupon.setCouponType(ur.getStatus());

                Coupon item = IdOperators.find(coupons, ur.getCouponId());
                if(item!=null){
                    coupon.setCouponName(item.getName());
                    coupon.setCouponType(item.getType());
                }
                coupon.setAmount(ur.getAmount());
                return coupon;
            }).collect(Collectors.toList());
            vo.setCouponUseRecords(playerFeeCoupons);
            return vo;
        }).collect(Collectors.toList());
    }

    @Transactional
    @Override
    public void deletePlayer(@NotNull Token token, @Nullable Long playerId) {
        token.validAnyAuthority(RoleEnum.CarpoolMaster, RoleEnum.CustomerService, RoleEnum.DM);
        Player player = playerService.select(playerId);
        ErrorCode.RECORD_NOT_EXIST.assertNotNull(player, playerId);
        Carpool carpool = carpoolService.select(player.getCarpoolId());
        ErrorCode.RECORD_NOT_EXIST.assertNotNull(carpool, player.getCarpoolId());
        ErrorCode.ORG_UNIT_ACCESS_DENIED.assertEqual(carpool.getOrgUnitId(), token.getOrgUnitId(),carpool.getOrgUnitId());
        ErrorCode.PROPERTY_CHECK_ERROR.assertFalse(carpool.ended(),"状态","拼车已经结束,不能移除玩家");
        deletePlayerFees(player);
        playerService.delete(playerId);
    }

    /**
     * 删除玩家的所有收费项
     * @param player 玩家对象
     */
    private void deletePlayerFees(@NotNull Player player) {
        List<PlayerFee> fees = playerFeeService.listByProperty("playerId",player.getId());
        Set<Long> feeIds = fees.stream().map(PlayerFee::getId).collect(Collectors.toSet());
        List<CouponUseRecord> useRecords = listInProperty("playerFeeId", feeIds);
        ErrorCode.PROPERTY_CHECK_ERROR.assertEmpty(useRecords,"优惠券","玩家使用了优惠券,不能删除");
        playerFeeService.deleteInProperty("id", feeIds);
    }

    private CouponUseRecordVo po2vo(CouponUseRecord item, List<SimpleOrgUnitVo> orgUnits, List<Carpool> carpools, List<PlayerFee> playerFees, List<SimpleUser> users) {
        CouponUseRecordVo vo = new CouponUseRecordVo();
        BeanUtils.copyProperties(item, vo);
        Carpool carpool = IdOperators.find(carpools, item.getCarpoolId());
        if(carpool != null){
            ShopMovie shopMovie=shopMovieService.select(carpool.getShopMovieId());
            if(shopMovie!= null)
            {
                vo.setCarpoolMoveName(shopMovie.getMovieName());
            }
            vo.setCarpoolBeginTime(carpool.getBeginTime());
            vo.setCarpoolTitle(carpool.getTitle());
            vo.setCarpoolStartTime(carpool.getStartTime());
        }

        SimpleOrgUnitVo orgUnit = IdOperators.find(orgUnits, item.getOrgUnitId());
        if(orgUnit != null){
            vo.setOrgUnitName(orgUnit.getName());
        }
        SimpleUser user = IdOperators.find(users, item.getUserId());
        if(user != null){
            vo.setCouponUserNickname(user.getNickname());
            vo.setCouponUserPhoneNumber(PhoneNumberUtils.encPhoneNumber(user.getPhoneNumber()));
        }
        user = IdOperators.find(users, item.getConfirmUserId());
        if(user != null){
            vo.setConfirmUserNickname(user.getNickname());
            vo.setConfirmUserPhoneNumber(PhoneNumberUtils.encPhoneNumber(user.getPhoneNumber()));
        }
        PlayerFee fee = IdOperators.find(playerFees, item.getPlayerFeeId());
        if(fee != null){
            SimpleUser UsingUser=movieServiceInvoker.userInfo(fee.getPlayerId());
            if(UsingUser!=null)
            {
                vo.setUsingUserNickname(UsingUser.getNickname());
                vo.setUsingUserPhoneNumber(UsingUser.getPhoneNumber());
            }
            vo.setFeeName(fee.getFeeName());
            vo.setFeeAmount(fee.getFeeAmount());

        }
        return vo;
    }
}
